package com.jt.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jt.dto.UserPageDto;
import com.jt.enums.SysLogEnum;
import com.jt.mapper.RoleMapper;
import com.jt.mapper.RouterMapper;
import com.jt.pojo.Result;
import com.jt.pojo.Role;
import com.jt.pojo.Router;
import com.jt.pojo.User;
import com.jt.service.RoleService;
import com.jt.service.RouterService;
import com.jt.service.UserService;
import com.jt.utils.JwtUtils;
import com.jt.utils.log.SysLogAnnotation;
import com.jt.vo.UserPageVo;
import com.mysql.cj.util.StringUtils;
import jdk.nashorn.internal.parser.Token;
import lombok.extern.slf4j.Slf4j;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;

    @Autowired
    private RouterService routerService;


    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 查询所有用户
     *
     * @param
     * @return Result
     */
    @RequestMapping("/selectAllUserList")
    @SysLogAnnotation(operModul = "用户模块", operType = SysLogEnum.OTHER, operDesc = "查询所有用户信息")
    public Result selectAllUserList() {
        List<User> list = userService.selectAllUserList();
        return new Result().setData(list).setCode(200).setMasege("查询所有list");
    }


    /**
     * 用户登录
     *
     * @param user 用户
     * @return Result
     */
    @RequestMapping("/login")
    @SysLogAnnotation(operDesc = "用户登录", operType = SysLogEnum.OTHER, operModul = "用户登录")
    public Result login(@RequestBody User user) {
        User resUser = userService.selectByNameAndPassWorld(user.getName(), user.getPassWold());
        if (!Objects.isNull(resUser)) {
            String token = JwtUtils.getToken(resUser);
            Map<String, Object> map = new HashMap<>();
            String roleId = resUser.getRoleId();
            if (!StringUtils.isNullOrEmpty(roleId)) {
                List<String> routerIdList = roleService.selectRouterIdListByRoleId(roleId);
                List<Router> router = routerService.selectRouterByRouterList(routerIdList);
                map.put("router", router);
            }
            map.put("token", token);
            redisTemplate.opsForValue().set("token", token, 60, TimeUnit.MINUTES);
            System.out.println(redisTemplate.opsForValue().get("token"));
            return new Result().setData(map).setCode(200).setMasege("登陆成功");
        }
        return new Result().setData(null).setCode(200).setMasege("没有此用户");
    }

    /**
     * 用户新增
     *
     * @param user
     * @return Result
     */
    @PostMapping("/insert")
    public Result insert(@RequestBody User user) {
        boolean trueAndFalse = userService.insert(user);
        if (!trueAndFalse) {
            return new Result().setData(false).setCode(200).setMasege("新增失败");
        }
        return new Result().setData(true).setCode(200).setMasege("新增成功");
    }

    /**
     * 用户分页列表
     *
     * @param userPageDto
     * @return userPageVo
     */
    @PostMapping("/pageList")
    @SysLogAnnotation(operDesc = "用户分页列表", operType = SysLogEnum.SELECT, operModul = "用户分页列表")
    public Result UserPageList(@RequestBody UserPageDto userPageDto) {
        userPageDto.setPageNumber((userPageDto.getPageNumber() - 1) * userPageDto.getPageSize());
        List<User> userList = userService.UserPageList(userPageDto);
        int total = userService.UserTotal(userPageDto);
        log.info("用户列表数据:{}和总条数:{}", userList, total);
        UserPageVo userPageVo = new UserPageVo();
        userPageVo.setUser(userList);
        userPageVo.setTotal(total);
        return new Result().setData(userPageVo).setCode(200).setMasege("分页查询成功");
    }

    @GetMapping("/getUserNameByToken")
    public String getUserNameByToken(String token) {
        Map<String, Object> map = JwtUtils.JieXi(token);
        return map.get("username").toString();
    }

    /**
     * Assign permissions用户分配角色
     */
    @GetMapping("/AssignPermissions")
    public Result AssignPermissions(String userId, String roleId) {
        boolean yesAndNo = userService.AssignPermissions(userId, roleId);
        if (!yesAndNo) {
            return new Result().setData(null).setMasege("分配角色失败 ").setCode(500);
        }
        return new Result().setData(null).setMasege("分配角色成功").setCode(200);
    }
}
